let App = getApp();

Page({
  data: {
    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    imgHeights: {}, // 图片的高度
    imgCurrent: {}, // 当前banne所在滑块指针

    // 页面元素
    items: {},
    bannerList: [], // banner列表
    designersList: [], // 设计师列表
    caseList: [], // 精选案例列表
    sceneList: [], // 施工现场列表
    newest: {},
    best: {},

    scrollTop: 0,
    name: '', // 姓名
    phone: '', // 手机号
    measure: '' // 房屋面积
  },

  onLoad: function() {
    // 设置页面标题
    App.setTitle();
    // 设置navbar标题、颜色
    App.setNavigationBar();
    // 获取首页banner
    this.getBanner();
    // 获取设计大咖
    this.getDesigners()
    // 获取精选案例
    this.getCases()
    // 获取施工现场
    this.getConstruction()
  },

  /**
   * 获取首页banner
   */
  getBanner: function() {
    let that = this;
    App._get('threesomes.pictures/index', {pos_id: 1}, function(res) {
      that.setData({
        bannerList: res.data.list
      });
    });
  },

  /**
   * 计算图片高度
   */
  imagesHeight: function(e) {
    let imgId = e.target.dataset.id,
      itemKey = e.target.dataset.itemKey,
      ratio = e.detail.width / e.detail.height, // 宽高比
      viewHeight = 750 / ratio, // 计算的高度值
      imgHeights = this.data.imgHeights;

    // 把每一张图片的对应的高度记录到数组里
    if (typeof imgHeights[itemKey] === 'undefined') {
      imgHeights[itemKey] = {};
    }
    imgHeights[itemKey][imgId] = viewHeight;
    // 第一种方式
    let imgCurrent = this.data.imgCurrent;
    if (typeof imgCurrent[itemKey] === 'undefined') {
      imgCurrent[itemKey] = Object.keys(this.data.items[itemKey].data)[0];
    }
    this.setData({
      imgHeights,
      imgCurrent
    });
  },
  // 设计大咖
  toDesigner(){
    wx.navigateTo({
      url: '/pages/designer/index',
    })
  },
  /**
   * 获取设计团队列表
   */
  getDesigners(){
    let that = this;
    let designersList = []
    App._get('threesomes.designers/index', {}, function(res) {
      designersList = res.data.list
      that.setData({
        designersList: designersList
      });
    });
  },
  // 设计师详情
  toDesignerDetail(e){
    wx.navigateTo({
      url: '/pages/designerDetail/index?id='+e.currentTarget.dataset.id,
    })
  },
  // 精选案例
  toCase(){
    wx.navigateTo({
      url: '/pages/case/index',
    })
  },
  /**
   * 获取精选案例列表
   */
  getCases(){
    let that = this;
    let caseList = []
    App._get('threesomes.cases/index', {}, function(res) {
      caseList = res.data.list
      that.setData({
        caseList: caseList
      });
    });
  },
  // 精选案例详情
  toCaseDetail(e){
    wx.navigateTo({
      url: '/pages/caseDetail/index?id='+e.currentTarget.dataset.id,
    })
  },
  // 施工现场
  toConstruction(){
    wx.navigateTo({
      url: '/pages/cnstruction/index',
    })
  },
  
  /**
   * 获取施工现场列表
   */
  getConstruction(){
    let that = this;
    let sceneList = []
    App._get('threesomes.scene/index', {}, function(res) {
      sceneList = res.data.list
      that.setData({
        sceneList: sceneList
      });
    });
  },
  // 施工现场详情
  toConstructionDetail(e){
    wx.navigateTo({
      url: '/pages/cnstructionDetail/index?id='+e.currentTarget.dataset.id,
    })
  },
  fillName(e){
    const that = this
    that.setData({
      name: e.detail.value
    })
  },
  fillPhone(e){
    const that = this
    that.setData({
      phone: e.detail.value
    })
  },
  fillMeasure(e){
    const that = this
    that.setData({
      measure: e.detail.value
    })
  },
  /**
   * 免费获取装修报价
   */
  getOffer(){
    console.log(1)
    const that = this
    let name = that.data.name
    let phone = that.data.phone
    let measure = that.data.measure
    let phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/
    if(name === ''){
      App.toastShow(that, "姓名不能为空", "icon_hint")
      return false;
    }else if(phone === ''){
      App.toastShow(that, "手机号不能为空", "icon_hint")
      return false;
    }else if(!phoneReg.test(phone)){
      App.toastShow(that, "请输入正确的手机号", "icon_hint")
      return false;
    }else if(measure === ''){
      App.toastShow(that, "房屋面积不能为空", "icon_hint")
      return false;
    }else{
      App._post_form('threesomes.consult/add', {name : name, phone: phone, area: measure}, function(res) {
        if(res.code == 1){
          that.setData({
            name: '',
            phone: '',
            measure: ''
          })
          App.toastShow(that, "提交成功，我们会尽快联系您", "icon_hint")
        }else{
          App.toastShow(that, "获取装修报价失败", "icon_hint")
        }
      });
    }
  },

  bindChange: function(e) {
    let itemKey = e.target.dataset.itemKey,
      imgCurrent = this.data.imgCurrent;
    // imgCurrent[itemKey] = e.detail.current;
    imgCurrent[itemKey] = e.detail.currentItemId;
    this.setData({
      imgCurrent
    });
  },

  goTop: function(t) {
    this.setData({
      scrollTop: 0
    });
  },

  scroll: function(t) {
    this.setData({
      indexSearch: t.detail.scrollTop
    }), t.detail.scrollTop > 300 ? this.setData({
      floorstatus: !0
    }) : this.setData({
      floorstatus: !1
    });
  },

  onShareAppMessage: function() {
    return {
      title: "小程序首页",
      desc: "",
      path: "/pages/index/index"
    };
  }
});